<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix" style="text-align: left;">
      <span>{{role == 3 ? "课程信息" :"课程信息"}}</span>
    </div>
    <div style="display: flex;align-items: center;margin-bottom:10px" v-if="role != 3">
      <el-input
        style="width: 250px;margin-right: 10px;"
        placeholder="请输入课程名称"
        v-model="name"
        clearable
      ></el-input>
      <el-button type="primary" @click="query">查询</el-button>
      <el-button type="primary" @click="add">新增课程</el-button>
    </div>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="courseNo" label="课程号"></el-table-column>
      <el-table-column prop="name" label="课程名"></el-table-column>
      <el-table-column prop="teacher.realName" label="授课教师"></el-table-column>
      <el-table-column prop="year" label="开课学期"></el-table-column>
      <el-table-column prop="period" label="学时"></el-table-column>
      <el-table-column prop="score" label="学分"></el-table-column>

      <el-table-column label="操作" width="120">
        <template scope="scope">
          <el-button type="text" v-if="role != 3" size="small" @click="modify(scope.row)">修改</el-button>
          <el-button type="text" v-if="role != 3" size="small" @click="deletes(scope.row.id)">删除</el-button>
          <el-button type="text" v-if="role == 3" size="small" @click="xuanke(scope.row.id)">选课</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="text-align:left;margin-top:25px"
      background
      layout="prev, pager, next"
      :total="totalCount"
      @current-change="getPage"
    ></el-pagination>

    <el-dialog :title="title" :visible.sync="show" width="500px">
      <el-form label-width="100px" size="small">
        <el-form-item label="课程名" :required="true">
          <el-input style="width: 280px;" v-model="course.name"></el-input>
        </el-form-item>
        <el-form-item label="授课教师" :required="true">
          <el-select v-model="course.teacherId" placeholder="请选择" style="width:280px" v-if="!update">
            <el-option
              v-for="item in teacher"
              :key="item.id"
              :label="item.realName"
              :value="item.id"
            ></el-option>
          </el-select>
          <el-select v-model="course.teacher.id" placeholder="请选择" style="width:280px" v-if="update">
            <el-option
              v-for="item in teacher"
              :key="item.id"
              :label="item.realName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开课学期" :required="true">
          <el-input style="width: 280px;" v-model="course.year"></el-input>
        </el-form-item>
        <el-form-item label="学时" :required="true">
          <el-input style="width: 280px;" v-model="course.period"></el-input>
        </el-form-item>
        <el-form-item label="学分" :required="true">
          <el-input style="width: 280px;" v-model="course.score"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit()">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </el-card>
</template>

<script>
import { request } from "../request.js";
export default {
  name: "course",
  data() {
    return {
      name: "",
      role: localStorage.getItem("role"),
      tableData: [],
      pageNo: 1,
      pageSize: 10,
      totalCount: 0,

      title: "新增课程",
      show: false,
      update: false,
      course: {
        teacherId: "",
        name: "",
        score: "",
        period: "",
        year: ""
      },
      teacher: []
    };
  },

  created() {
    this.getData();
    request.request("teacher/getTeacherNameAndId", "POST", {}).then(data => {
      console.log(data.data);
      this.teacher = data.data;
    });
  },
  methods: {
    /**
     * 查询
     */
    query() {
      this.pageNo = 1;
      this.getData();
    },

    /**
     * 获取分页数据
     */
    getData() {
      let param = {
        name: this.name,
        pageNo: this.pageNo,
        pageSize: this.pageSize
      };

      request
        .request("/course/getCourseList", "POST", param, this)
        .then(data => {
          data = data.data;
          this.tableData = data.list;
          this.pageNo = data.page.currentPage;
          this.totalPage = data.page.totalCount;
          console.log(data);
        });
    },

    /**
     * 分页器翻页
     */
    getPage(page) {
      this.pageNo = page;
      this.getData();
    },
    /**
     * 显示新增的模态框
     */
    add() {
      this.show = true;
      this.title = "新增课程";
    },

    /**
     * 提交新增/编辑
     */
    submit() {
      if (!this.course.name) {
        this.$message.error("请输入课程名称");
        return;
      }

      if (!this.update && !this.course.teacherId) {
        this.$message.error("请选择授课教师");
        return;
      }
      if (this.update && !this.course.teacher.id) {
        this.$message.error("请选择授课教师");
        return;
      }
      if (!this.course.year) {
        this.$message.error("请输入开课学期");
        return;
      }
      if (!this.course.period) {
        this.$message.error("请输入课程学时");
        return;
      }
      if (!this.course.score) {
        this.$message.error("请输入课程学分");
        return;
      }
      this.update && (this.course.teacherId = this.course.teacher.id) 
      request
        .request(
          this.update ? "course/update" : "course/add",
          "POST",
          this.course,
          this
        )
        .then(() => {
          this.course.name = "";
          this.course.subject = "";
          this.course.sex = "";
          this.$message.success(this.update ? "更新成功" : "新增成功");
          this.show = false;
          this.query();
        });
    },

    /**
     * 显示编辑的模态框
     */
    modify(course) {
      this.course = course;
      this.update = true;
      this.show = true;
      this.title = "学生信息编辑";
    },

    /**
     * 删除
     */
    deletes(id) {
      request.request("course/delete", "POST", { id: id }, this).then(() => {
        this.$message.success("删除成功");
        this.query();
      });
    },

    /**
     * 学生选课
     */
    xuanke(id) {
      request.request("course/xuanKe", "POST", { id: id }, this).then(() => {
        this.$message.success("选课成功");
        this.query();
      });
    }
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
</style>
